package org.example.rejectmsg;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: anlu
 * @Date: 2021/06/22/16:51
 * @Description:存放到延迟队列的元素，对业务数据进行了包装
 */
public class RejectProducer {
    public final static String EXCHANGE_NAME = "RejectExchange";
    public final static String QUEUE_NAME = "rejectQueue_fouceserror";
    public static void main(String[] args)throws IOException, TimeoutException {
        /**
         * 创建连接连接到RabbitMQ
         */
        ConnectionFactory factory = new ConnectionFactory();
        // 设置MabbitMQ所在主机ip或者主机名
        factory.setHost("127.0.0.1");

        // 创建一个连接
        Connection connection = factory.newConnection();
        // 创建一个信道
        Channel channel = connection.createChannel();
        // 指定转发
        channel.exchangeDeclare(EXCHANGE_NAME, "direct");
        String[] serverities = {"error","info","warning"};
        //所有日志严重性级别
        for(int i=0;i<10;i++){
            String routekey = serverities[i%3];
            // 发送的消息
            String message = "Hello World_"+(i+1);
            //参数1：exchange name
            //参数2：routing key
            channel.basicPublish(EXCHANGE_NAME, routekey,
                    null, message.getBytes());
            System.out.println(" [x] Sent "+routekey+":'"
                    + message + "'");
        }
        // 关闭频道和连接
        channel.close();
        connection.close();

    }

}
